import service from './service'
import request from '@/utils/request'

interface ApiResponse<T = any> {
  code: number
  message?: string
  data: T
}

interface LoginParams {
  Email: string
  Password: string
}

interface RegisterParams {
  username: string
  email: string
  role: string
  password: string
  phone?: string
  real_name: string
  organization?: string
  department?: string
  research_direction?: string[]
}

export const loginAPI = (data: LoginParams): Promise<ApiResponse<{
  token: string
  refresh_token: string
  user: {
    id: number
    username: string
    email: string
    role: string
    real_name?: string
    phone?: string
  }
}>> => {
  return request({
    url: '/auth/login',
    method: 'post',
    data
  })
}

export const registerAPI = (data: RegisterParams): Promise<ApiResponse<{
  username: string
  email: string
  role: string
  status: string
}>> => {
  return request({
    url: '/auth/register',
    method: 'post',
    data
  })
}


export const sendEmailCode = (email: string): Promise<{
  code: number;
  message: string;
  data: {
    success: boolean;
    message: string;
    vcode?: string; // 仅在开发环境下返回验证码
  };
  trace_id?: string;
}> => {
  return request({
    url: '/email/send',
    method: 'post',
    data: { 
      email,
      type: 0 // 0: 验证码邮件
    }
  })
}

export const verifyEmailCode = (email: string, code: string): Promise<{
  code: number;
  message: string;
  data: null;
  trace_id?: string;
}> => {
  return request({
    url: '/email/validate',
    method: 'post',
    data: { email, code }
  })
}

export const refreshAuthAPI = (refreshToken: string): Promise<ApiResponse<any>> => {
  return request({
    url: '/auth/refresh',
    method: 'post',
    data: { refresh_token: refreshToken }
  })
}

export const logoutAPI = (): Promise<ApiResponse<any>> => {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}

export const getUserInfoAPI = () => {
  return service.get('/users/me')
}

export const updateUserInfoAPI = (data: {
  nickname?: string
  avatar?: string
  phone?: string
  bio?: string
}) => {
  return service.put('/users/me', data)
}

export const changePasswordAPI = (data: {
  old_password: string
  new_password: string
}) => {
  return service.put('/users/me/password', data)
}

export const modPasswordAPI = async (old_password: string, new_password: string) => {
  return await service.put('/users/me/password', {
    old_password,
    new_password,
  })
}

export const getUserDetailAPI = async (id: number) => {
  return await service.get(`/users/${id}`)
}

//撤回申请
export const withdrawApplyAPI = async (id: number) => {
  return await service.delete(`/applications/${id}`)
}

export const getMyApplyAPI = () => {
  return service.get('/applications/my')
}

export const getMatchListAPI = () => {
  return service.get('/matches')
}

export const getMyMatchAPI = async () => {
  return await service.get('/matches/my')
}

export const confirmMatchAPI = async (id: number) => {
  return await service.post(`/matches/${id}/confirm`)
}

export const getNotificationAPI = async () => {
  return await service.get('/notifications')
}

export const getUnreadNotificationCountAPI = async () => {
  return await service.get('/notifications/unread/count')
}

export const changeToReadAPI = async (id: number) => {
  return await service.put(`/notifications/${id}/read`)
}
